package cn.sirun.com.fca.personal.center.view.impl;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tencent.cloud.iov.common.flow.view.impl.BaseLoadingFlowView;
import com.tencent.cloud.iov.flow.view.IContentViewContainer;
import com.tencent.cloud.iov.kernel.constant.StatusCode;
import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.router.Router;
import com.tencent.cloud.iov.util.ActivityUtils;
import com.tencent.cloud.iov.util.ResourcesUtils;
import com.tencent.cloud.iov.util.storage.TXSharedPreferencesUtils;
import com.tencent.cloud.uikit.utils.DialogUtils;
import com.tencent.cloud.uikit.utils.SystemUiUtils;

import cn.sirun.com.fca.account.constant.AccountActivityConst;
import cn.sirun.com.fca.account.constant.AccountIntentConst;
import cn.sirun.com.fca.account.constant.CarConst;
import cn.sirun.com.fca.account.model.base.CarInfoDetailResponse;
import cn.sirun.com.fca.personal.center.R;
import cn.sirun.com.fca.personal.center.constant.PersonalCenterActivityConst;
import cn.sirun.com.fca.personal.center.model.request.SubmitUnbindCarRequest;
import cn.sirun.com.fca.personal.center.presenter.ICarManagerPresenter;
import cn.sirun.com.fca.personal.center.presenter.impl.CarManagerPresenter;
import cn.sirun.com.fca.personal.center.view.ICarInfoView;

public class CarInfoFlowView extends BaseLoadingFlowView<ICarManagerPresenter>
        implements ICarInfoView {

    private ImageView mCarIV, mSwitchIV, mAuthIV;
    private TextView mBrandTV, mVinTV, mDistributorTV, mCarNumTV, mStatusTV, mLineTV, mDeleteTV,
            mDeletePromptTV;
    private LinearLayout mCarNumLL, mLineLL, mSettingCurCarLl, mVerifyLL;
    private LinearLayout mTransferLL, mWorthlessLL, mVehicleLL, mOtherLL;
    private ImageView mTransferIV, mWorthlessIV, mVehicleIV, mOtherIV;
    private CarInfoDetailResponse mCarInfo;
    private int mUnbindCause;

    public CarInfoFlowView(Activity activity) {
        super(activity);
    }

    public CarInfoFlowView(Activity activity, IContentViewContainer container) {
        super(activity, container);
    }

    @Override
    public void onSetCurCarResult(BaseResponse<Void> response) {
        hideLoadingView();
        int code = response.getCode();
        if (code == StatusCode.SUCCESS) {
            mSwitchIV.setBackgroundResource(R.drawable.ic_switch_on);
            return;
        }
        DialogUtils.showToast(getActivity(), response.getMsg());
    }

    @Override
    public void onDeleteCarResult(BaseResponse<Void> response) {
        int code = response.getCode();
        if (code == StatusCode.SUCCESS) {
            getPresenter().getCarInfo(mCarInfo.getDin());
            return;
        }
        hideLoadingView();
        DialogUtils.showToast(getActivity(), response.getMsg());
    }

    @Override
    public void onCarInfoResult(BaseResponse<CarInfoDetailResponse> response) {
        hideLoadingView();
        int code = response.getCode();
        if (code == StatusCode.SUCCESS) {
            mCarInfo = response.getData();
            initData();
        }
    }

    @Override
    protected ICarManagerPresenter createPresenter() {
        return new CarManagerPresenter();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mCarInfo = (CarInfoDetailResponse) getActivity().getIntent()
                .getSerializableExtra(CarConst.SpKey.CAR_INFO_DETAIL);
        setUpActionBar();
        initView();
    }

    @Override
    public void onStart() {
        super.onStart();
        showLoadingView();
        getPresenter().getCarInfo(mCarInfo.getDin());
    }

    private void setUpActionBar() {
        final Activity activity = getActivity();
        SystemUiUtils.showActionBar(
                activity, "Alfaromeo",
                ResourcesUtils.getDrawable(R.color.white));
    }

    private void initView() {
        setContentView(R.layout.car_info_activity);
        final Activity activity = getActivity();
        mCarIV = activity.findViewById(R.id.iv_car_bg);
        mSwitchIV = activity.findViewById(R.id.iv_car_setting_car);
        mAuthIV = activity.findViewById(R.id.iv_auth_into);

        mBrandTV = activity.findViewById(R.id.tv_car_brand);
        mVinTV = activity.findViewById(R.id.tv_car_vin);
        mDistributorTV = activity.findViewById(R.id.tv_car_distributor);
        mCarNumTV = activity.findViewById(R.id.tv_car_num);
        mStatusTV = activity.findViewById(R.id.tv_car_status);
        mLineTV = activity.findViewById(R.id.tv_line);
        mDeleteTV = activity.findViewById(R.id.tv_delete_car);
        mDeletePromptTV = activity.findViewById(R.id.tv_delete_prompt);

        mCarNumLL = activity.findViewById(R.id.ll_car_num);
        mLineLL = activity.findViewById(R.id.ll_hot_line);
        mSettingCurCarLl = activity.findViewById(R.id.ll_setting_cur_car);
        mVerifyLL = activity.findViewById(R.id.ll_verify);

        mCarNumLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = Router.fromPath(PersonalCenterActivityConst.Path.CAR_NUM);
                intent.putExtra(CarConst.SpKey.CAR_PLATE_NUMBER, mCarInfo.getPlateNumber());
                intent.putExtra(CarConst.SpKey.CAR_ACTIVATE_INFO, mCarInfo.getDin());
                ActivityUtils.startActivityForResult(activity, intent,
                        CarConst.SETTING_CAR_PLATE_NUMBER_RESULT);
            }
        });

        mSwitchIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!TextUtils.isEmpty(mCarInfo.getDin())) {
                    if (!TXSharedPreferencesUtils.getValue(CarConst.SpKey.CAR_ACTIVATE_INFO)
                            .equals(mCarInfo.getDin())) {
                        showLoadingView();
                        getPresenter().setCurrentVehicle(mCarInfo.getDin());
                    }
                }
            }
        });

        mDeleteTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deleteDialog();
            }
        });

        mVerifyLL.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent intent = Router.fromPath(AccountActivityConst.Path.USER_ARGEEMENT);
                intent.putExtra("din", mCarInfo.getDin());
                ActivityUtils.startActivityForResult(getActivity(), intent);
            }
        });
        initData();
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == CarConst.SETTING_CAR_PLATE_NUMBER_RESULT) {
            String plateNumber = data.getExtras().getString(CarConst.SpKey.CAR_PLATE_NUMBER);
            mCarNumTV.setText(plateNumber);
        } else if (requestCode == AccountIntentConst.DELETE_CAR_VERIFY_PIN_RESULT_CODE) {
            showLoadingView();
            SubmitUnbindCarRequest request = new SubmitUnbindCarRequest(mCarInfo.getDin()
                    , mUnbindCause);
            getPresenter().unbindCar(request);
        }
    }

    private void initData() {

        int availableStatus = mCarInfo.getAvailableStatus();

        mBrandTV.setText(String.format(ResourcesUtils.getString(R.string.car_manager_status), "Alfaromeo"));
        mVinTV.setText(String.format(ResourcesUtils.getString(R.string.car_manager_vin), mCarInfo.getVin()));
        if (mCarInfo.getReseller() != null) {
            mDistributorTV.setText(String.format(ResourcesUtils.getString(R.string.car_info_distributor),
                    mCarInfo.getReseller().getProvince() + mCarInfo.getReseller().getCity() + "  "
                            + mCarInfo.getReseller().getName()));
        } else {
            mDistributorTV.setText(String.format(ResourcesUtils.getString(R.string.car_info_distributor),
                    ResourcesUtils.getString(R.string.no_fill_in)));
        }

        if (availableStatus == 0) {
            mStatusTV.setText(R.string.id_not_verified);
            mCarNumLL.setVisibility(View.GONE);
            mSettingCurCarLl.setVisibility(View.GONE);
            mStatusTV.setTextColor(ResourcesUtils.getColor(R.color.blue_5C));
            mVerifyLL.setEnabled(true);
        } else if (availableStatus == 1) {
            mStatusTV.setText(R.string.verifying);
            mCarNumLL.setVisibility(View.GONE);
            mSettingCurCarLl.setVisibility(View.GONE);
            mVerifyLL.setEnabled(false);
            mVerifyLL.setClickable(false);
        } else if (availableStatus == 2) {
            mStatusTV.setText(R.string.id_verified);
            if (!TextUtils.isEmpty(mCarInfo.getPlateNumber())) {
                mCarNumTV.setText(mCarInfo.getPlateNumber());
            }
            mAuthIV.setVisibility(View.GONE);
            mVerifyLL.setEnabled(false);
            mVerifyLL.setClickable(false);
        } else if (availableStatus == 5) {
            mStatusTV.setText(R.string.delete_auth);
            mStatusTV.setTextColor(ResourcesUtils.getColor(R.color.red_EC));
            mCarNumLL.setVisibility(View.GONE);
            mSettingCurCarLl.setVisibility(View.GONE);
            mVerifyLL.setEnabled(false);
            mVerifyLL.setClickable(false);
            mDeleteTV.setVisibility(View.GONE);
            mDeletePromptTV.setVisibility(View.VISIBLE);
        }

        if (!TextUtils.isEmpty(mCarInfo.getDin())) {
            if (TXSharedPreferencesUtils.getValue(CarConst.SpKey.CAR_ACTIVATE_INFO)
                    .equals(mCarInfo.getDin())) {
                mSwitchIV.setBackgroundResource(R.drawable.ic_switch_on);
            } else {
                mSwitchIV.setBackgroundResource(R.drawable.ic_switch_off);
            }
        }
    }

    private void deleteDialog() {
        LayoutInflater inflater = (LayoutInflater) getActivity()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.common_dialog, null, false);

        LinearLayout layout = view.findViewById(R.id.dialog_layout);
        TextView contentView = view.findViewById(R.id.tv_title);
        TextView sureView = view.findViewById(R.id.tv_sure);
        TextView cancleView = view.findViewById(R.id.tv_tel_cancle);
        contentView.setText(ResourcesUtils.getString(R.string.delete_car));

        final Dialog dialog = new Dialog(getActivity(), R.style.dialog_activity);
        dialog.setCancelable(true);
        dialog.setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        dialog.show();

        sureView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.cancel();
                selectReasonDialog();
            }
        });

        cancleView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.cancel();
            }
        });
    }

    private void selectReasonDialog() {
        LayoutInflater inflater = (LayoutInflater) getActivity()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.unbind_car_reason_dialog, null, false);

        LinearLayout layout = view.findViewById(R.id.reason_layout);
        mTransferLL = view.findViewById(R.id.ll_reason_transfer);
        mWorthlessLL = view.findViewById(R.id.ll_reason_worthless);
        mVehicleLL = view.findViewById(R.id.ll_reason_vehicle_transation);
        mOtherLL = view.findViewById(R.id.ll_reason_other);
        TextView nextTV = view.findViewById(R.id.tv_next);
        mTransferIV = view.findViewById(R.id.iv_reason_transfer);
        mWorthlessIV = view.findViewById(R.id.iv_reason_worthless);
        mVehicleIV = view.findViewById(R.id.iv_reason_vehicle);
        mOtherIV = view.findViewById(R.id.iv_reason_other);

        hanldeReasonClick(R.drawable.ic_reason_select, R.drawable.ic_car_manager_uncur_car
                , R.drawable.ic_car_manager_uncur_car, R.drawable.ic_car_manager_uncur_car);

        final Dialog dialog = new Dialog(getActivity(), R.style.dialog_activity);
        dialog.setCancelable(true);
        dialog.setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        dialog.show();

        mTransferLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mUnbindCause = 0;
                hanldeReasonClick(R.drawable.ic_reason_select, R.drawable.ic_car_manager_uncur_car
                        , R.drawable.ic_car_manager_uncur_car, R.drawable.ic_car_manager_uncur_car);
            }
        });

        mWorthlessLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mUnbindCause = 1;
                hanldeReasonClick(R.drawable.ic_car_manager_uncur_car, R.drawable.ic_reason_select,
                        R.drawable.ic_car_manager_uncur_car, R.drawable.ic_car_manager_uncur_car);
            }
        });

        mVehicleLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mUnbindCause = 2;
                hanldeReasonClick(R.drawable.ic_car_manager_uncur_car, R.drawable.ic_car_manager_uncur_car,
                        R.drawable.ic_reason_select, R.drawable.ic_car_manager_uncur_car);
            }
        });

        mOtherLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mUnbindCause = 3;
                hanldeReasonClick(R.drawable.ic_car_manager_uncur_car, R.drawable.ic_car_manager_uncur_car,
                        R.drawable.ic_car_manager_uncur_car, R.drawable.ic_reason_select);
            }
        });

        nextTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.cancel();
                Intent intent = Router.fromPath(AccountActivityConst.Path.PIN_VALIDATE);
                intent.putExtra(AccountIntentConst.FROM, AccountIntentConst.DELETE_CAR_VERIFY_PIN);
                ActivityUtils.startActivityForResult(getActivity(), intent,
                        AccountIntentConst.DELETE_CAR_VERIFY_PIN_RESULT_CODE);

            }
        });
    }

    private void hanldeReasonClick(int isTransfer, int isWorthless, int isVehicle,
                                   int isOther) {
        mTransferIV.setBackgroundResource(isTransfer);
        mWorthlessIV.setBackgroundResource(isWorthless);
        mVehicleIV.setBackgroundResource(isVehicle);
        mOtherIV.setBackgroundResource(isOther);
    }

}
